package controller;

import model.User;
import org.apache.log4j.Logger;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import service.UserServiceI;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.math.BigDecimal;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * Created by Administrator on 2016/6/15.
 */
@Controller
public class UserController {
    private static final Logger log = Logger.getLogger(LoginController.class);
    @Resource
    private UserServiceI userServiceI;

    /**
     * 跳转到demo.jsp页面
     * @return
     */
    @RequestMapping(value = "userManager")
    public String toManager(){
        return "demo";
    }
    /**
     * 返回用户列表
     * @return
     */
    @RequestMapping(value = "userList")
    @ResponseBody
    public Map<String,Object> userList(HttpServletRequest request){
        Map<String,Object> map = new HashMap<String, Object>();
        User user = new User();
        String order = "CREATE_TIME desc ";
        String pageNow = request.getParameter("page");
        String pageSize = request.getParameter("rows");
        List<User> list = userServiceI.getUser(Integer.parseInt(pageNow), Integer.parseInt(pageSize), user, order);
        int count = userServiceI.getUserCount(user);
        map.put("total",count);
        Object[] arr = {};
        if(!list.isEmpty()){
            arr = list.toArray();
        }
        map.put("rows",arr);
        return map;
    }



    /**
     * 添加用户
     * @return
     */
    @RequestMapping(value = "addUser")
    @ResponseBody
    public Map<String,Object> addUser(User user){
        Map<String,Object> map = new HashMap<String, Object>();
        BigDecimal userId = user.getId();
        int result = 0;
        if(null == userId){
            result = userServiceI.saveUser(user);
        }else{
            result = userServiceI.updateUserById(user);
        }
        if(1 == result){
            map.put("result","success");
        }else {
            map.put("result","fail");
        }
        return map;
    }

    /**
     * 根据userId获取用户
     * @return
     */
    @RequestMapping(value = "editUser")
    @ResponseBody
    public User getUser(String userId){
        Map<String,Object> map = new HashMap<String, Object>();
        return userServiceI.getUserById(new BigDecimal(userId));
    }

    /**
     * 根据userId删除用户
     * @return
     */
    @RequestMapping(value = "delUser")
    @ResponseBody
    public Map<String,Object> delUser(String userId){
        Map<String,Object> map = new HashMap<String, Object>();
        User user = userServiceI.getUserById(new BigDecimal(userId));
        user.setDeleted("1");
        int result = userServiceI.updateUserById(user);
        if(1 == result){
            map.put("result","success");
        }else{
            map.put("result","fail");
        }
        return map;
    }
}
